package com.vitabuybuy.tv.ui.cart

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.BR
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityOrderPayBinding
import com.vitabuybuy.tv.ui.cart.viewmodel.ConfirmOrderViewModel
import kotlinx.android.synthetic.main.activity_order_pay.*

class OrderPayActivity : BaseActivity<ConfirmOrderViewModel, ActivityOrderPayBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_order_pay

    companion object {
        const val FROM_PAY_ORDER = 1001
        const val FROM_BIDDING = 1002
        fun openActivity(mContext: BaseActivity<*, *>, id: Int?, amount: Double?, from: Int) {
            mContext.startActivity(
                bundleOf(
                    Pair("id", id),
                    Pair("amount", amount),
                    Pair("from", from)
                ),
                OrderPayActivity::class.java
            )
        }

        fun openActivity(
            mContext: BaseActivity<*, *>,
            amount: Double,
            biddingId: Int,
            addressId: Int,
            from: Int
        ) {
            mContext.startForResult(
                bundleOf(
                    Pair("amount", amount),
                    Pair("biddingId", biddingId),
                    Pair("addressId", addressId),
                    Pair("from", from)
                ), 1001,
                OrderPayActivity::class.java
            )
        }
    }

    private val PAY_BY_WECHAT = "微信"
    private val PAY_BY_ALIPAY = "支付宝"
    private val PAY_BY_BANK = "银联"
    private var id: Int? = -1
    private var biddingId: Int? = -1
    private var addressId: Int? = -1
    private var amount: Double? = 0.00
    private var from: Int? = FROM_PAY_ORDER
    private var paymentMethod = PAY_BY_WECHAT
    override fun onGetBundle(bundle: Bundle?) {
        from = bundle?.getInt("from")
        when (from) {
            FROM_PAY_ORDER -> {
                id = bundle?.getInt("id")
                amount = bundle?.getDouble("amount")
            }
            FROM_BIDDING -> {
                biddingId = bundle?.getInt("biddingId")
                addressId = bundle?.getInt("addressId")
                amount = bundle?.getDouble("amount")
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.pay))
        mBinding.setVariable(BR.amount, amount)
    }


    override fun eventHandling() {
        super.eventHandling()
        mIvBack.setOnClickListener {
            onBackPressed()
        }
        btnPay.setOnClickListener {
            when (from) {
                FROM_PAY_ORDER -> {
                    viewModel.payOrder(id, paymentMethod)
                }
                FROM_BIDDING -> {
                    viewModel.joinBidding(biddingId, addressId, paymentMethod)
                }
            }
        }
        rg_choose_pay.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rb_wechat_pay -> paymentMethod = PAY_BY_WECHAT
                R.id.rb_ali_pay -> paymentMethod = PAY_BY_ALIPAY
                R.id.rb_bank -> paymentMethod = PAY_BY_BANK
            }
        }
    }

    private var dialog: AlertDialog? = null

    override fun onBackPressed() {
        dialog = AlertDialog.Builder(mContext)
            .setTitle(getString(R.string.reminder))
            .setMessage(getString(R.string.are_you_sure_give_up_pay))
            .setCancelable(true)
            .setPositiveButton(getString(R.string.go_on_pay), null)
            .setNegativeButton(getString(R.string.cancle_pay)) { _, _ ->
                finish()
            }.create()
        dialog!!.show()
    }

    override fun onDestroy() {
        super.onDestroy()
        if (dialog != null) {
            dialog!!.dismiss()
        }
    }


    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            2 -> {
                PaySuccessActivity.openActivity(mContext, id, amount)
                finish()
            }
            3 -> {
                finishResult(null)
            }
        }
    }
}
